<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MX_Controller {
	
	protected $id = 0;
	
	protected $record_per_page = 0;
	
	protected $validation = array(
								array(
									'field' => 'title',
									'label' => 'lang:admin_blog_title',
									'rules' => 'trim|htmlspecialchars|required|max_length[100]|callback__check_title'
								),
								array(
									'field' => 'url',
									'label' => 'lang:admin_blog_url',
									'rules' => 'trim|required|alpha_dot_dash|max_length[100]|callback__check_url'
								),
								array(
									'field' => 'type',
									'label' => 'lang:admin_blog_type',
									'rules' => 'trim|required'
								),
								array(
									'field' => 'excerpt',
									'label' => 'lang:admin_blog_excerpt',
									'rules' => 'trim|required'
								),
									array(
									'field' => 'body',
									'label' => 'lang:admin_blog_body',
									'rules' => 'trim|required'
								)
							);
	
    public function __construct()
    {
        parent::__construct();
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		$this->load->library('users/auth');
		$this->load->library('form_validation');
		$this->load->library('pagination');
        $this->form_validation->CI =& $this;
		
		$this->lang->load('users/user');
		$this->lang->load('blog/blog');
		
		$this->load->model('blog_m', 'blog');
		$this->load->model('users/users_m', 'user');
		
		$this->template
				->set_theme('default')
				->set_layout('default')
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation', 'partials/admin/menu')
				->set_breadcrumb('Home', 'admin/dashboard');
    }
	
	public function index()
	{
		$this->create();
	}
	
	public function dashboard()
	{
		if (!$this->auth->is_logged_in() === TRUE)
		{
			redirect('users/login', 'refresh');
		}
		
		$this->template
				->build('admin/create');
	}
	
	/**
	 * Create a new post
	 */
	public function create()
	{
		if (!$this->auth->is_logged_in())
		{
			redirect('users/login', 'refresh');
		}
		
		$this->lang->load('admin');
		
		// Set the validation rules
		$this->form_validation->set_rules($this->validation);
		
		if ($this->form_validation->run())
		{
			$oPost = new Blog_m();
			$oPost->author 				= $this->session->userdata('id');
			$oPost->date_posted 		= ($this->input->post('status') == 'live') ? date('Y-m-d') : '1970-01-01';
			$oPost->title				= $this->input->post('title');
			$oPost->url_title			= $this->input->post('url');
			$oPost->excerpt				= $this->input->post('excerpt');
			$oPost->content				= $this->input->post('body');
			$oPost->category_id			= $this->input->post('category');
			$oPost->date_created 		= now();
			$oPost->comments_enabled 	= '0';
			$oPost->status				= $this->input->post('status');
			$oPost->type				= $this->input->post('type');
			$oPost->is_active			= '1';
			$oPost->save();
		}
		
		$this->load->model('blog_categories_m', 'categories');
		
		$categories = $this->categories->get_categories();
		
		$this->template
				->set_breadcrumb('Post','admin/lists/')
				->set_breadcrumb(' Create')
				->append_metadata($this->asset->js('jscripts/tiny_mce/tiny_mce.js'))
				->append_metadata($this->asset->js('tiny_mce_init.js'))
				->append_metadata($this->asset->js('jquery.tab.init.js'))
				->append_metadata($this->asset->js('jquery/facebox.js'))
				->append_metadata($this->asset->css('admin/jquery.tab.css'))
				->append_metadata($this->asset->css('facebox.css'))
                ->set_partial('header', 'partials/header')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation', 'partials/admin/menu')
				->set('categories', $categories)
                ->set_layout('default')
				->build('admin/post/create');
	}
	
	public function edit($id = NULL)
	{
		if (!$this->auth->is_logged_in())
		{
			redirect('users/login', 'refresh');
		}
		
		$this->lang->load('admin');
		
		// Set the validation rules
		$this->form_validation->set_rules($this->validation);
		
		if ($this->form_validation->run())
		{
			$oPost = new Blog_m();
			$oPost->author 				= $this->session->userdata('id');
			$oPost->date_posted 		= ($this->input->post('status') == 'live') ? date('Y-m-d') : '1970-01-01';
			$oPost->title				= $this->input->post('title');
			$oPost->url_title			= $this->input->post('url');
			$oPost->excerpt				= $this->input->post('excerpt');
			$oPost->content				= $this->input->post('body');
			$oPost->category_id			= $this->input->post('category');
			$oPost->date_created 		= now();
			$oPost->comments_enabled 	= '0';
			$oPost->status				= $this->input->post('status');
			$oPost->type				= $this->input->post('type');
			$oPost->is_active			= '1';
			$oPost->save();
		}
	}
	
	public function lists($page = 0)
	{
		$this->lang->load('admin');
		
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('admin/admin/lists');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$posts = new Blog_m();
			
			$total_rows = $posts->where('is_active', '1')->get(($limit + $page), $page)->count();
			
			if($total_rows < $page)
			{
				redirect('admin/admin/lists');
			}
			else
			{
				$data['posts']				= $posts->where('is_active', '1')->get($limit, $page);
				
				//setup pagination
				$config['base_url']			= site_url('admin/admin/lists/');
				$config['per_page']			= $limit;
				$config['uri_segment'] 		= 4;
				$config['total_rows']		= $total_rows;
				$config['anchor_class'] 	= 'class="btn"';
				$config['cur_tag_open'] 	= ' <div class="btn btn-disabled">';
				$config['cur_tag_close'] 	= '</div>';
				
				$this->pagination->initialize($config);
				
				$data['page'] 				= $this->pagination->create_links();
				
				$this->template
							->set_breadcrumb('Posts','')
							->build('admin/post/list',$data);
			}
		}
	}
	
	public function user_listing()
	{
		//$this->blog->get_user_posts();
		$this->blog->get_user_posts_categories();
		//$this->blog->get_user_posts($id = 1);
	}
	
	
	
	public function _check_title($title = '')
	{
		//if ( ! $this->post_m->check_exists('title', $title))
		//{
		//	return FALSE;
		//}
		//
		//return TRUE;
	}
	
	public function _check_url($url = '')
	{
		//if ( ! $this->post_m->check_exists('slug', $url)
		//{
		//	return FALSE;
		//}
		//
		//return TRUE;
	}
}

/* End of file admin.php */
/* Location: ./application/modules/users/controllers/admin.php */